import java.sql.SQLException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class PersonManage {
    People p;
    Scanner sc =new Scanner(System.in);
    //个人管理
    public PersonManage(){}
    //个人主页
    public void home(People p) throws InterruptedException, SQLException, ClassNotFoundException {
        this.p=p;
        int x;
        while(true) {
            menuCenter();
            x = sc.nextInt();
            switch (x) {
                case 1://修改密码--id
                    changePassword();
                    break;
                case 2://修改用户名--id
                    changeName();
                    break;
                case 3://修改电话
                    changeTele();
                    break;
                case 4://注销
                    logout();
                    break;
                case 0://返回
                    break;
                default:
                    System.out.println("非法输入");
                    break;
            }
            if(x==0||x==4){
                break;
            }
        }
    }
    //修改用户名--id
    public void changeName() throws InterruptedException, SQLException, ClassNotFoundException {
        Base ba=new Base();
        System.out.print("请输入新用户名:");
        while(true) {
            String name = sc.next();
            if(name.length()<30){
                p.setName(name);
                //改数据库---------------------
                ba.updatePerson(p);
                System.out.println("修改成功");
                break;
            }else{
                System.out.println("用户名过长!");
                System.out.print("请重新输入新用户名:");
            }
        }
        TimeUnit.SECONDS.sleep(1);
        System.out.flush();
    }
    //修改密码--id
    public void changePassword() throws InterruptedException, SQLException, ClassNotFoundException {
        Base ba=new Base();
        String npassword=null;
        boolean flag=false;
        for(int i=3;i>0;i--){
            if(i!=3){
                System.out.println("原密码输入错误!还有"+i+"次机会");
            }
            System.out.print("请输入原密码(按-1返回):");
            String  password=sc.next();//原密码
            if(password.equals(p.getPassword())){
                while(true) {
                    if(npassword==null){
                        String p;
                        System.out.print("请输入新密码:");
                        p=sc.next();
                        //检查密码是否合法？-----------------------

                        npassword= p;
                    }else {
                        System.out.print("请再次输入新密码:");
                        //修改
                        if (npassword.equals(sc.next())) {
                            p.setPassword(npassword);
                            //改数据库
                            ba.updatePerson(p);
                            flag = true;
                            break;
                        } else {
                            System.out.println("新密码输入错误!");
                        }
                    }

                }
            }else if(password.equals("-1")){
                flag=true;
            }
            if(flag){
                break;
            }
        }
    }
    //修改电话--id
    public void changeTele() throws InterruptedException, SQLException, ClassNotFoundException {
        Base ba=new Base();
        System.out.print("请输入新电话号码:");
        while(true) {
            String tele = sc.next();
            if(tele.length()<30){
                p.setTele(tele);
                //改数据库---------------------
                ba.updatePerson(p);
                System.out.println("修改成功");
                break;
            }else{
                System.out.println("非法电话号码!");
                System.out.print("请重新输入新电话号码:");
            }
        }
    }
    //注销--id
    public void logout() throws InterruptedException, SQLException, ClassNotFoundException {
        Base ba=new Base();
        System.out.print("请输入密码:");
        for(int i=3;i>0;i--){
            if(i!=3){
                System.out.println("密码错误！还有"+i+"次机会");
                System.out.print("请重新输入密码:");
            }
            if(p.getPassword().equals(sc.next())){
                System.out.print("请再次确定是否注销账号(Y/N):");
                String s=sc.next();
                if(s.equals("Y") || s.equals("y")){
                    p.setType(-1);
                    //该数据库-----------------------
                    ba.deleteUser(p.getId());
                    break;
                }else{
                    System.out.println("注销失败");
                    break;
                }
            }
        }
    }
    //菜单
    public void menuCenter(){
        System.out.println("---------个人中心---------");
        System.out.println("账号:"+p.getId());
        System.out.println("用户名:"+p.getName());
        System.out.println("电话号码:"+p.getTele());
        System.out.print("用户类型:");
        if(p.getType()==0){
            System.out.println("管理员");
        }else{
            System.out.println("普通用户");
        }
        System.out.println("1.修改密码 2.修改用户名");
        System.out.println("3.修改电话 4.注销用户 ");
        System.out.println("0.返回");
        System.out.print("请选择=>");
    }
}
